package com.zhouxianglh.p24;

/**
 * 24. 两两交换链表中的节点
 * <p>
 * 注意空值判断
 *
 * @author zhouxianglh
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        if (null == head || head.next == null) {
            return head;
        }

        ListNode first = head;
        ListNode second = head.next;

        ListNode temp = second;
        second = first;
        first = temp;

        head = first;

        temp = first.next;
        first.next = second;
        second.next = temp;
        //
        while (null != temp && null != temp.next) {
            second.next = temp.next;
            first = temp;
            second = temp.next;

            temp = second;
            second = first;
            first = temp;

            temp = first.next;
            first.next = second;
            second.next = temp;

        }
        return head;
    }


    public class ListNode {
        int val;

        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
